package com.joker.aigc.accountboolk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joker.aigc.accountboolk.domain.dto.AccountBookDTO;
import com.joker.aigc.accountboolk.domain.dto.AccountBookQueryDTO;
import com.joker.aigc.accountboolk.domain.mapstruct.AccountBookMapstruct;
import com.joker.aigc.accountboolk.domain.po.AccountBookPO;
import com.joker.aigc.accountboolk.domain.vo.AccountBookVO;
import com.joker.aigc.accountboolk.mapper.AccountBookMapper;
import com.joker.aigc.accountboolk.service.AccountBookService;
import com.joker.aigc.utils.AssertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 账本服务实现类
 * @author Joker
 * @since 2025/07/21
 */
@Service
@Slf4j
public class AccountBookServiceImpl extends ServiceImpl<AccountBookMapper, AccountBookPO> implements AccountBookService {


    @Resource
    private AccountBookMapstruct accountBookMapstruct;

    /**
     * 列表
     *
     * @param accountBookQueryDTO 查询条件
     * @return 符合条件的数据
     */
    @Override
    public List<AccountBookVO> list(AccountBookQueryDTO accountBookQueryDTO) {
        log.info("开始查询参数:{}", accountBookQueryDTO);
        // 开始查询
        List<AccountBookPO> accountBookPage = baseMapper.selectList(new LambdaQueryWrapper<AccountBookPO>()
                .eq(Objects.nonNull(accountBookQueryDTO.getUserId()), AccountBookPO::getUserId, accountBookQueryDTO.getUserId())
                .eq(Objects.nonNull(accountBookQueryDTO.getType()), AccountBookPO::getType, accountBookQueryDTO.getType())
                .eq(Objects.nonNull(accountBookQueryDTO.getCategory()), AccountBookPO::getCategory, accountBookQueryDTO.getCategory())
                .between(Objects.nonNull(accountBookQueryDTO.getStartTime()), AccountBookPO::getDate, accountBookQueryDTO.getStartTime(), accountBookQueryDTO.getEndTime()));
        return accountBookMapstruct.toVO(accountBookPage);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(AccountBookDTO accountBookVO) {
        AssertUtil.isNotEmpty(accountBookVO, "参数不能为空");
        AssertUtil.isEmpty(accountBookVO.getId(), "id必须为空");
        AssertUtil.isNotEmpty(accountBookVO.getUserId(), "用户id不能为空");
        AccountBookPO entity = accountBookMapstruct.toEntity(accountBookVO);
        log.info("插入数据：{}", entity);
        baseMapper.insert(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateById(AccountBookDTO accountBookVO) {
        AssertUtil.isNotEmpty(accountBookVO.getId(), "id不能为空");
        AccountBookPO entity = accountBookMapstruct.toEntity(accountBookVO);
        baseMapper.updateById(entity);
        log.info("更新数据：{}", entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateBatchById(List<AccountBookDTO> accountBookVO) {
        AssertUtil.isNotEmpty(accountBookVO, "参数不能为空");
        List<AccountBookPO> entityList = accountBookMapstruct.toEntity(accountBookVO);
        baseMapper.updateById(entityList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteBatchById(List<Long> idList) {
        AssertUtil.isNotEmpty(idList, "参数不能为空");
        baseMapper.deleteByIds(idList);
    }
}
